﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.Data;


public partial class Month_AddMonthRemind : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    Month_RemindofManager dllMonth_RemindofManager = new Month_RemindofManager();
    UserInfoManager dllUserInfo = new UserInfoManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    DataTable _userInfo = new DataTable();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    public string _realName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.RegisterStartupScript("getheighandwidth", "<script>document.getElementById('h1h').value=window.screen.availHeight;document.getElementById('h2w').value= window.screen.width ;</script>");

        if (!IsPostBack)
        {
           // bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
            bindDropDownList(this.drpMonthDate);
            ListItem _li = this.drpMonthDate.SelectedItem;
            if (_li != null)
            {
                if (_li.Text != "无月周期")
                {
                    bindGridView(_li);
                }
                else
                {
                    this.gdvMonthRemind.DataSource = createTable();
                    this.gdvMonthRemind.DataBind();
                }
            }
        }

        this.btnAdd.Enabled = false;
        this.btnDel.Enabled = false;
        this.btnModify.Enabled = false;
        this.btnDisplay.Enabled = false;
        //判断权限，计划管理员拥有修改权
        if (objUserInfo.UserName == "admin")
        {
            this.btnAdd.Enabled = true;
            this.btnDel.Enabled = true;
            this.btnModify.Enabled = true;
            this.btnDisplay.Enabled = true;
        }
        //判断是否为经理，非经理锁定部门选择框        
        int _isManager = dllAuthentication.IsManager(objUserInfo.UserName);
        if (_isManager > 0)
        {
            this.btnAdd.Enabled = true;
        }

    }


    /// <summary>
    /// 生成空白行
    /// </summary>
    /// <returns></returns>
    DataTable createTable()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("RemindId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PlanSponsor");
        _dt.Columns.Add(dc);
        dc = new DataColumn("EndDate");
        _dt.Columns.Add(dc);
        dc = new DataColumn("RemindPeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("DepartmentName");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CreateTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MonthDate");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PrvMonthRemind");
        _dt.Columns.Add(dc);
        dc = new DataColumn("TheMonthRemind");
        _dt.Columns.Add(dc);
        dc = new DataColumn("IsDelete");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["RemindId"] = "";
        dr["PlanSponsor"] = "";
        dr["EndDate"] = "";
        dr["RemindPeople"] = "";
        dr["DepartmentName"] = "";
        dr["CreateTime"] = "";
        dr["MonthDate"] = "";
        dr["PrvMonthRemind"] = "";
        dr["TheMonthRemind"] = "";
        dr["IsDelete"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    void bindGridView(ListItem _li)
    {
        object[] _user = new object[1];
        _user[0] = objUserInfo.UserName;
        DataTable _usertable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
        if (_usertable.Rows.Count > 0)
        {
            _realName = _usertable.Rows[0]["RealName"].ToString();
        }
        if (Request.QueryString["param"] == null)
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(_li.Value);
             
            DataTable _monthRemind = new Month_RemindofManager().get(Month_RemindofManager.field.DepartmentName.ToString(), Month_RemindofManager.field.MonthDate.ToString(), _value);
            this.gdvMonthRemind.DataSource = _monthRemind;
            this.gdvMonthRemind.DataBind();

            if (this.gdvMonthRemind.Rows.Count == 0)
            {
                this.gdvMonthRemind.DataSource = createTable();
                this.gdvMonthRemind.DataBind();
            }
        }
        else
        {
            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            this.gdvMonthRemind.DataSource = new Month_RemindofManager().getMonthRemindByUser(_value);
            this.gdvMonthRemind.DataBind();
            
        }
    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 绑定所有的dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        //部门绑定
        //if (_drp.Equals(this.drpDepartment))
        //{
        //    this.drpDepartment.DataSource = new BLL.DepartmentInfoManager().getNoTree();
        //    this.drpDepartment.DataTextField = "DepartmentName";
        //    this.drpDepartment.DataValueField = "DepartmentId";
        //    this.drpDepartment.DataBind();
        //    ListItem _li = new ListItem("请选择部门");
        //    this.drpDepartment.Items.Insert(0, _li);
        //    object[] _value = new object[1];
        //    _value[0] = objUserInfo.UserName;
        //    DataTable _userTable = new BLL.DepartmentInfoManager().get(UserInfoManager.field.UserName.ToString(), _value);
        //    if (_userTable.Rows.Count > 0)
        //    {
        //        this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(_userTable.Rows[0]["DepartmentId"].ToString()));

        //    }
        //    if (Request.QueryString["depart"] != null)
        //    {
        //        this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByValue(Request.QueryString["depart"].ToString()));
        //    }
        //}
        //年份绑定
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            if (Request.QueryString["Year"] == null)
            {
                DataTable _year = dllQuarterDateManager.getQuarterDate();
                this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));
            }
            else
            {
                this.drpYear.SelectedValue = Utility.HttpHelp.getParamsString("Year");
            }
        }


        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                if (Request.QueryString["QuarterDate"] == null)
                {
                    #region dropdownlist显示系统所对应的当前年周期
                    //根据系统当前日期查找到对应的年周期
                    DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                    //如果查到结果（结果只有一条）

                    if (_quartertime.Rows.Count == 1)
                    {
                        this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                        this.drpQuarterDate.DataSource = _quartertime;
                        this.drpQuarterDate.DataTextField = "QuarterName";
                        this.drpQuarterDate.DataValueField = "Quarter_Id";
                    }
                    #endregion
                }
                else
                {
                    this.drpQuarterDate.SelectedValue = Utility.HttpHelp.getParamsString("QuarterDate");
                }
                //bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem _li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(_li);
                bindDropDownList(this.drpMonthDate);
            }

        }


        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                if (Request.QueryString["MonthDate"] == null)
                {
                    #region 根据系统当前日期绑定到当前月周期
                    DataTable _monthtime = dllMonthDateManager.getMonthDate();
                    if (_monthtime.Rows.Count > 0)
                    {
                        this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));

                    }
                    #endregion
                }
                else
                {
                    this.drpMonthDate.SelectedValue = Utility.HttpHelp.getParamsString("MonthDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
                this.gdvMonthRemind.DataSource = createTable();
                this.gdvMonthRemind.DataBind();
            }
        }
    }

    /// <summary>
    /// 添加月握手关系
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if ( this.txtSponsor.Value.Length > 0
            && this.txtTheMonthRemind.Text.Length > 0
            && this.txtRemindPeople.Value.Length > 0
            && this.txtEndDate.Text.Length > 0)
        {
            object[] _value = new object[5];
            _value[0] = this.txtSponsor.Value;
            
            _value[1] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _value[2] = this.txtTheMonthRemind.Text;
            _value[3] = this.txtRemindPeople.Value;
            _value[4] = Convert.ToDateTime(this.txtEndDate.Text);
            int val = new BLL.Month_RemindofManager().add(_value);
            if (val > 0)
            {
                ListItem li = this.drpMonthDate.SelectedItem;
                bindGridView(li);
            }
        }
        else
        {
            Utility.HttpHelp.alert("请填写完整", this.Page);
            return;
        }
    }



    /// <summary>
    /// 年改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindDropDownList(this.drpMonthDate);
    }


    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
    }

    /// <summary>
    /// 月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpMonthDate.SelectedItem;
        if (_li != null)
        {
            if (_li.Text != "无月周期")
            {
                bindGridView(_li);
            }
            else
            {
                this.gdvMonthRemind.DataSource = createTable();
                this.gdvMonthRemind.DataBind();
            }
        }
    }




    /// <summary>
    /// 自动编号
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvMonthRemind_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }

    /// <summary>
    /// 提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引

    }

    /// <summary>
    /// 修改月握手关系
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        int count = 0;
        object[] _value = new object[1];
        for (int i = 0; i < this.gdvMonthRemind.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthRemind.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                count++;
                _value[0] = Convert.ToInt32(this.gdvMonthRemind.DataKeys[i]["RemindId"]);
            }
        }
        if (count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个工作提醒", Page);

        }
        if (count == 0)
        {
            Utility.HttpHelp.alert("请至少选择一个工作提醒", Page);

        }
        if (count == 1)
        {
            string _url = "ModifyMonthRemind.aspx?Id=" + _value[0] + "&Year=" + this.drpYear.SelectedValue + "&QuarterDate=" + this.drpQuarterDate.SelectedValue + "&MonthDate=" + this.drpMonthDate.SelectedValue + "&url=AddMonthRemind.aspx";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','top=100,left=150,width=500,height=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");

        }

    }


    /// <summary>
    /// 工作提醒投影事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string _url = "MonthRemindDisplay.aspx";
        string _height = this.h1h.Value;
        string _width = this.h2w.Value;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height= " + _height + ",width=" + _width + ",top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");

        ListItem _li = this.drpMonthDate.SelectedItem;
        if (_li != null)
        {
            bindGridView(_li);
        }
    }




    protected void btnDel_Click(object sender, EventArgs e)
    {
        object[] _value = new object[1];
        int count = 0;
        for (int i = 0; i < this.gdvMonthRemind.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthRemind.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                count++;
                _value[0] = Convert.ToInt32(this.gdvMonthRemind.DataKeys[i]["RemindId"]);
            }
        }
        if (count == 1)
        {

            int val = new BLL.Month_RemindofManager().del(BLL.Month_RemindofManager.field.RemindId.ToString(), _value);
            if (val > 0)
            {
                ListItem _li = this.drpMonthDate.SelectedItem;
                if (_li != null)
                {
                    bindGridView(_li);
                }
            }
            else
            {
                Utility.HttpHelp.alert("删除失败", this.Page);
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择一个", this.Page);
        }
    }


    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpMonthDate.SelectedItem;
        if (_li != null)
        {
            bindGridView(_li);
        }
        bindGridView(_li);
    }
}